from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash

from . import db


class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间




class Member(BaseModel, db.Model):
    """会员中心"""
    __tablename__ = "info_user"

    id = db.Column(db.Integer, primary_key=True)  # 会员编号
    password_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    member_type  = db.Column(  # 会员类型
        db.Enum(
            "supplier",  # 供应商
            "dealer",  # 经销商
            "masses"   # 群众
        ),
        default="supplier")
    company_name = db.Column(db.String(256), nullable=False)   # 公司名
    location = db.Column(   # 所在地
        db.Enum(
            "guangxisheng",  # 广西省
            "jilinsheng",  # 吉林省
            "guangdongsheng"  # 广东省
        ),
        default="guangxisheng")
    email = db.Column(db.String(256))  # 邮箱
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    is_admin = db.Column(db.Boolean, default=False)  # 管理员
    gender = db.Column(  # 性别
        db.Enum(
            "MAN",  # 男
            "WOMAN"  # 女
        ),
        default="MAN")

    @property
    def password(self):
        raise AttributeError("当前属性不允许读取")

    @password.setter
    def password(self, value):
        # self.password_hash = 对value加密
        self.password_hash = generate_password_hash(value)

    def check_password(self, password):
        """校验密码"""
        return check_password_hash(self.password_hash, password)

class OrderOnline(BaseModel, db.Model):
    """在线订购"""
    __tablename__ = "order_online"

    id = db.Column(db.Integer, primary_key=True)  # 订单编号
    product_name = db.Column(db.String(256), nullable=False)  # 产品名称
    product_number = db.Column(db.String(256), nullable=False)  # 产品数量
    user_name = db.Column(db.String(256), nullable=False)  # 收货姓名
    gender = db.Column(  # 性别
        db.Enum(
            "MAN",  # 男
            "WOMAN"  # 女
        ),
        default="MAN")
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号码
    postalcode = db.Column(db.String(6), unique=True, nullable=False)  # 邮政编码
    telephone = db.Column(db.String(11), unique=True, nullable=False)  # 固定电话
    email = db.Column(db.String(256))  # 电子邮箱
    address = db.Column(db.String(256), nullable=False)  # 地址
    remark = db.Column(db.String(256))  #  其他说明





class News(BaseModel, db.Model):
    """新闻"""
    __tablename__ = "info_news"

    id = db.Column(db.Integer, primary_key=True)  # 新闻编号
    title = db.Column(db.String(256), nullable=False)  # 新闻标题
    digest = db.Column(db.String(512), nullable=False)  # 新闻摘要
    content = db.Column(db.Text, nullable=False)  # 新闻内容
    index_image_url = db.Column(db.String(256))  # 新闻列表图片路径
    category_id = db.Column(db.Integer, db.ForeignKey("news_category.id"))


    def to_basic_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "digest": self.digest,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "index_image_url": self.index_image_url
        }
        return resp_dict

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "digest": self.digest,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "category": self.category.to_dict(),
            "index_image_url": self.index_image_url,
        }
        return resp_dict


class Product(BaseModel, db.Model):
    """产品"""
    __tablename__ = "info_product"

    id = db.Column(db.Integer, primary_key=True)  # 产品编号
    product_name = db.Column(db.String(256), nullable=False)  # 产品名称
    english_name = db.Column(db.String(256), nullable=False)  # 产品英文名称
    degree = db.Column(db.String(256))      # 产品度数
    year = db.Column(db.String(4))          # 产品年份
    origin = db.Column(db.String(256))      # 产品产地
    standard = db.Column(db.String(256))    # 产品规格
    type = db.Column(db.String(256))        # 产品类别
    content = db.Column(db.Text, nullable=False)  # 产品详情
    index_image_url = db.Column(db.String(256))  # 产品图片路径
    category_id = db.Column(db.Integer, db.ForeignKey("product_category.id"))  # 产品分类

    def to_basic_dict(self):
        resp_dict = {
            "id": self.id,
            "product_name": self.product_name,
            "english_name": self.english_name,
            "index_image_url": self.index_image_url
        }
        return resp_dict

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "degree": self.degree,
            "year": self.year,
            "origin": self.origin,
            "standard": self.standard,
            "type": self.type,
            "content": self.content,
            "category": self.category.to_dict(),
            "index_image_url": self.index_image_url,
        }
        return resp_dict


class ProductNews(BaseModel, db.Model):
    """探秘产品"""
    __tablename__ = "product_news"

    id = db.Column(db.Integer, primary_key=True)  # 产品新闻编号
    title = db.Column(db.String(256), nullable=False)  # 产品新闻标题
    digest = db.Column(db.String(512), nullable=False)  # 产品新闻摘要
    content = db.Column(db.Text, nullable=False)  # 产品新闻内容
    index_image_url = db.Column(db.String(256))  # 产品新闻列表图片路径
    category_id = db.Column(db.Integer, db.ForeignKey("product_news_category.id"))


    def to_basic_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "digest": self.digest,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "index_image_url": self.index_image_url
        }
        return resp_dict

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "digest": self.digest,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "category": self.category.to_dict(),
            "index_image_url": self.index_image_url,
        }
        return resp_dict



class NewsCategory(BaseModel, db.Model):
    """新闻分类"""
    __tablename__ = "news_category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    news_list = db.relationship('News', backref='category', lazy='dynamic')

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name
        }
        return resp_dict

class ProductCategory(BaseModel, db.Model):
    """产品分类"""
    __tablename__ = "product_category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    news_list = db.relationship('Product', backref='category', lazy='dynamic')

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name
        }
        return resp_dict


class ProductNewsCategory(BaseModel, db.Model):
    """产品新闻分类"""
    __tablename__ = "product_news_category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    news_list = db.relationship('Product', backref='category', lazy='dynamic')

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name
        }
        return resp_dict
